package com.navinfo.platform.thousandkilo.utils;

import com.navinfo.platform.basicdatastatistics.utils.day.VehicleBasicDataUtil;
import com.navinfo.platform.thousandkilo.dto.DayStatisticsDto;
import com.navinfo.platform.thousandkilo.dto.ThousandKiloData;
import com.navinfo.platform.thousandkilo.dto.TripInfoDto;
import org.apache.spark.sql.Row;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import scala.collection.mutable.WrappedArray;

import java.util.ArrayList;
import java.util.List;

public class ThousandKiloDataUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(VehicleBasicDataUtil.class);

    public static DayStatisticsDto parseRowToDayStatisticsDto(Row row) {
        DayStatisticsDto dto = new DayStatisticsDto();
        dto.setUid(row.getAs("uid"));
        dto.setTid(row.getAs("tid"));
        calcTripInfo(row, dto);
        return dto;
    }

    public static ThousandKiloData parseRowToThousandKiloData(Row row) {
        ThousandKiloData dto = new ThousandKiloData();
        dto.setUid(row.getAs("uid"));
        dto.setTid(row.getAs("tid"));
        dto.setMileageSection(getIntValue(row.getAs("mileageSection")));
        dto.setSectionStartFuel(getIntValue(row.getAs("sectionStartFuel")));
        dto.setSectionEndFuel(getIntValue(row.getAs("sectionEndFuel")));
        dto.setSectionFuel(getIntValue(row.getAs("sectionFuel")));
        dto.setSectionStartMileage(getIntValue(row.getAs("sectionStartMileage")));
        dto.setSectionEndMileage(getIntValue(row.getAs("sectionEndMileage")));
        dto.setSectionMileage(getIntValue(row.getAs("sectionMileage")));
        dto.setSectionUrea(getIntValue(row.getAs("sectionUrea")));
        return dto;
    }

    private static void calcTripInfo(Row row, DayStatisticsDto dayStatisticsDto) {
        WrappedArray.ofRef<Integer> tripIdList;
        WrappedArray.ofRef<Integer> routeStartMileageList;
        WrappedArray.ofRef<Integer> routeStartFuelList;
        WrappedArray.ofRef<Integer> routeEndMileageList;
        WrappedArray.ofRef<Integer> routeEndFuelList;
        WrappedArray.ofRef<Integer> tripUreaList;
        try {
            tripIdList = row.getAs("trip_id_list");
            routeStartMileageList = row.getAs("route_start_mileage_list");
            routeStartFuelList = row.getAs("route_start_fuel_list");
            routeEndMileageList = row.getAs("route_end_mileage_list");
            routeEndFuelList = row.getAs("route_end_fuel_list");
            tripUreaList = new WrappedArray.ofRef<>(new Integer[0]);//row.getAs("trip_urea_list"); 暂时不上线

            int tripIdSize = tripIdList.size();
            int routeStartMileageSize = routeStartMileageList.size();
            int routeStartFuelSize = routeStartFuelList.size();
            int routeEndMileageSize = routeEndMileageList.size();
            int routeEndFuelSize = routeEndFuelList.size();
            int tripUreaSize = tripUreaList.size();

            List<TripInfoDto> tripInfoDtos = new ArrayList<>();
//            for (int i = 0; i < tripIdSize; i++) {
//                tripInfoDtos.add(new TripInfoDto(tripIdList.apply(i),
//                        i < routeStartMileageSize ? routeStartMileageList.apply(i) : 0,
//                        i < routeStartFuelSize ? routeStartFuelList.apply(i) : 0,
//                        i < routeEndMileageSize ? routeEndMileageList.apply(i) : 0,
//                        i < routeEndFuelSize ? routeEndFuelList.apply(i) : 0,
//                        i < tripUreaSize ? tripUreaList.apply(i) : 0));
//            }
            dayStatisticsDto.setTripInfos(tripInfoDtos);

        } catch (UnsupportedOperationException | IllegalArgumentException e) {
            LOGGER.error(e.getMessage(), e);
        }
    }

    private static Integer getIntValue(Long value) {
        if (value == null) {
            return null;
        }
        return value.intValue();
    }
}
